
<template>
  <el-card>
    <!-- 数据表格开始 -->
    <el-table  border :data="noticeTableList" @selection-change="handleSelectionChnage">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="公告ID" align="center" prop="noticeId"/>
      <el-table-column label="公告标题" align="center" prop="noticeTitle"/>
      <el-table-column label="公告类型" prop="noticeType" align="center" :formatter="noticeTypeFormatter"/>
      <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter"/>
      <el-table-column label="发布者" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" :disabled="single" @click="handleDelete(scope.row)">删除
          </el-button>
          <el-button type="text" icon="el-icon-view" size="mini" @click="handleView(scope.row)">查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->
    <!-- 分页控件开始 -->
    <el-pagination
        :current-page="queryParams.pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryParams.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
    <!-- 分页控件结束 -->

    <!-- 添加修改弹出层开始 -->
    <el-dialog
        :title="title"
        :visible.sync="open"
        width="800px"
        center
        append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题" prop="noticeTitle">
              <el-input v-model="form.noticeTitle" placeholder="请输入通知公告标题" clearable
                        size="small"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                    v-for="dict in statusOptions"
                    :key="dict.dictValue"
                    :label="dict.dictValue"
                    :value="dict.dictValue"
                >{{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="noticeType">
              <el-radio-group v-model="form.noticeType">
                <el-radio
                    v-for="dict in noticeTypeOptions"
                    :key="dict.dictValue"
                    :label="dict.dictValue"
                    :value="dict.dictValue"
                >{{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="公告内容">
              <vue-editor v-model="form.noticeContent" :options="{hideModeSwitch:true,previewStyle:'tab'}"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" clearable size="small"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 添加修改弹出层结束 -->
    <!--公告内容 弹出层开始 -->
    <el-dialog
        :title="title"
        :visible.sync="noticeContentOpen"
        width="800px"
        center
        append-to-body
    >
      <vue-editor v-model="noticeContent"/>
    </el-dialog>
    <!-- 公告内容弹出层结束 -->
  </el-card>
</template>


<script>
import { VueEditor } from "vue2-editor";
export default {
  components: {
    VueEditor
  },
  // 定义页面数据
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: false,
      // 非多个禁用
      multiple: true,
      // 分页数据总条数
      total: 0,
      // 字典表格数据
      noticeTableList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 公告类型
      noticeTypeOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        noticeTitle: undefined,
        createBy: undefined,
        noticeType: undefined,
        status: undefined
      },
      // 表单数据
      form: {
      },
      // 表单校验
      rules: {
        noticeTitle: [
          {required: true, message: '通知公告标题不能为空', trigger: 'blur'}
        ]
      },
      // 查看内容的弹出层
      noticeContentOpen: false,
      // 查看内容
      noticeContent: undefined
    }
  },
  created() {
    this.getNoticeList()
    this.loadStatus();
    this.loadNoticeType();
  },
  methods: {
    // 查询表格数据
    getNoticeList() {
      this.$http.post("/system/notice/listByPage",this.queryParams).then(resp=>{
        console.log(resp.data.data)
        this.noticeTableList= resp.data.data.records;
        this.queryParams.total=resp.data.data.total;
      })
    },
    // 打开查看弹出层
    handleView(row) {
      this.$http.get("/system/notice/getByNoticeId/"+row.noticeId).then(resp=>{
        this.noticeContentOpen = true
        this.noticeContent=resp.data.data.noticeContent;
        this.title = row.noticeTitle
        console.log(this.noticeContent)
      })
    },

    // 执行删除
    handleDelete(row) {
      const noticeIds = row.noticeId || this.ids
      this.$confirm('此操作将永久删除该通知公告数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.get("/system/notice/delete/"+noticeIds).then(resp=>{
          this.$message.success("删除成功");
          this.getNoticeList()// 列表重新查询
        }).catch(error => {
          this.$message.error("删除失败");
          console.error('删除请求失败:', error);
        });
      }).catch(() => {
        this.$message.warning("取消删除");
      });
    },
    // 保存
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // 做添加
          this.loading = true
          if (this.form.noticeId === undefined) {
            this.$http.post("/system/notice/addUpdate", this.form).then(res => {
              this.$message.success('---添加成功---')
              this.getNoticeList() // 列表重新查询
              this.open = false // 关闭弹出层
            })
          } else { // 做修改
            this.$http.post("/system/notice/addUpdate", this.form).then(res => {
              this.$message.success('---修改成功---')
              this.open = false // 关闭弹出层
              this.getNoticeList() // 列表重新查询
            })
          }
        } else {
          this.$message.error('请填写完整的表单信息')
        }
      })
    },

    //加载状态
    loadStatus() {
      this.$http.get("/system/dict/data/type/sys_normal_disable").then(resp=>{
        this.statusOptions=resp.data.data;
      })
    },
    loadNoticeType() {
      this.$http.get("/system/dict/data/type/sys_notice_type").then(resp=>{
        this.noticeTypeOptions=resp.data.data;
      })
    },
    // 格式化状态
    statusFormatter(row){
      let statusName="未知";
      this.statusOptions.filter(item=>{
        if(row.status===item.dictValue){
          statusName=item.dictLabel;
        }
      })
      return statusName;
    },
    // 格式化类型
    noticeTypeFormatter(row){
      let noticeTypeName="未知";
      this.noticeTypeOptions.filter(item=>{
        if(row.noticeType===item.dictValue){
          noticeTypeName=item.dictLabel;
        }
      })
      return noticeTypeName;
    },
    // 打开添加的弹出层
    handleAdd() {
      this.form = {}
      this.open = true
      this.title = '---添加通知公告---'
    },

    // 打开修改的弹出层
    handleUpdate(row) {
      this.title = '---修改通知公告---'
      this.open = true
      this.form = row

    },
    // 条件查询
    handleQuery() {
      this.getNoticeList()
    },
    // 重置查询条件
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 5,
        noticeTitle: undefined,
        createBy: undefined,
        noticeType: undefined,
        status: undefined
      }
    },
    // 数据表格的多选择框选择时触发
    handleSelectionChnage(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length > 1
      this.multiple = selection.length === 0 || selection.length === null
    },
    // 分页pageSize变化时触发
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      // 重新查询
      this.getNoticeList()
    },
    // 点击上一页  下一页，跳转到哪一页面时触发
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      // 重新查询
      this.getNoticeList()
    },
    // 取消
    cancel() {
      this.open = false
      this.resetForm()
      this.getNoticeList()
    },
    resetForm() {
      this.$refs.form.resetFields()
    }

  }
}

</script>
